package util;

import java.io.*;
import java.util.Properties;

public class Common {

    public static String initDirectory(String path) {
        File dirCfg = new File(path);
        if(!dirCfg.exists()) dirCfg.mkdirs();

        return dirCfg.getAbsolutePath().replace('\\', '/');
    }

    public static Properties initConfig(Properties defaultConfig, String configPath) {

        BufferedReader reader = null;
        try {
            reader        = new BufferedReader(new InputStreamReader(new FileInputStream(configPath)));
            defaultConfig = new Properties();
            defaultConfig.load(reader);
            return defaultConfig;

        } catch (IOException e) {

            BufferedWriter writer = null;
            try {
                writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(configPath)));
                defaultConfig.store(writer, "我们的电商系统服务器默认配置文件");
                return defaultConfig;
            } catch (IOException e1) {
                System.err.print("写入配置文件失败，本次将使用默认配置属性。");
                System.err.println(e1.getMessage());
                return defaultConfig;
            } finally {
                if(writer != null) {
                    try {
                        writer.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            }
        } finally {
            if(reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static boolean isStringEmpty(String s) { return s == null  ||  s.isEmpty(); }
}
